<!DOCTYPE html>
<html>

<head>
	<title></title>
	<meta charset="UTF-8">
	<link rel="stylesheet" href="../../element-ui-2.13.0/lib/theme-chalk/index.css" />
	<script type="text/javascript" src="../../vue/vue-v2.6.10.js"></script>
	<script type="text/javascript" src="../../element-ui-2.13.0/lib/index.js"></script>
	<script type="text/javascript" src="../../vue/axios-0.18.0.js"></script>
</head>

<body>
	<div id="app">
		<el-tag type="info" effect="dark" v-if="inputVisible" closable="true" :disable-transitions="disable"
			@close="handleClose">课程管理</el-tag>
		<div v-if="inputVisible">
			<template>
				<el-table :data="tableData" @selection-change="handleSelectionChange" size="medium"
					highlight-current-row="true" style="width: 100%">
					<el-table-column type="selection" width="55" prop="cid">
					</el-table-column>
					<el-table-column width="100px" label="序号" type="index">
					</el-table-column>
					<el-table-column label="课程名称" prop="courseName">
					</el-table-column>
					<el-table-column label="课程类型" prop="courseType">
						<template slot-scope="{row}">
							<span v-if="row.courseType==1">Java</span>
							<span v-if="row.courseType==2">数据库</span>
							<span v-if="row.courseType==3">前端</span>
						</template>
					</el-table-column>
					<el-table-column label="课程价格" prop="coursePrice">
					</el-table-column>
					<el-table-column label="状态">
						<template slot-scope="{row}">
							<el-tag v-if="row.status==0">未上架</el-tag>
							<el-tag v-if="row.status==1" type="danger">上架</el-tag>
							<el-tag v-if="row.status==2" type="danger">下架</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="操作" width="300px">
						<template slot-scope="scope">
							<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
							<el-button size="mini" @click="handleLook(scope.$index, scope.row)">详情
							</el-button>
							<el-button size="mini" @click="addCourseDetail(scope.$index, scope.row)">添加明细</el-button>
						</template>
					</el-table-column>
					<el-table-column align="left" width="200px">
						<template slot="header" slot-scope="scope">
							<el-input v-model="search" @blur="findAll()" size="mini" placeholder="输入课程名称搜索" />
						</template>
					</el-table-column>
				</el-table>
			</template>
			<br />
			<el-row>
				<el-button type="warning" @click="delAll()">删除选中</el-button>
				<el-button type="primary">
					<a href="addCourse.html" target="main" style="text-decoration: none;color: white;">上传课程</a>
				</el-button>
			</el-row>
			<template>
				<div class="block" align="right">
					<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
						:current-page="currentPage" :page-sizes="[3, 4, 5, 6]" :page-size="pageSize"
						layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
					</el-pagination>
				</div>
			</template>

		</div>

		<!--修改课程-->
		<el-dialog title="收货地址" :visible.sync="dialogFormVisible1" :close-on-click-modal="false">
			<!--表单-->
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				<el-form-item label="课程名称" prop="courseName">
					<el-input v-model="ruleForm.courseName" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="课程简介" prop="descs">
					<el-input v-model="ruleForm.descs" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="课程类型" prop="courseType">
					<el-select v-model="ruleForm.courseType" placeholder="请选择课程类型">
						<el-option label="JavaEE" :value="1"></el-option>
						<el-option label="数据库" :value="2"></el-option>
						<el-option label="前端技术" :value="3"></el-option>
					</el-select>
				</el-form-item>
				<!--上传-->
				<el-upload style="margin-left: 25px;" :on-remove="handleRemove1" :on-exceed="handleExceed"
					class="upload-demo" ref="upload1" :data="{value:1}" :http-request="myUpload" :limit="1"
					:auto-upload="false">
					<el-button slot="trigger" size="small" type="primary">选取图片</el-button>
					<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload(1)">上传到服务器
					</el-button>
				</el-upload>
				<img :src="dialogImageUrl" style="width: 100px;height: 100px;margin-left: 25px;" alt="" />

				<el-upload style="margin-left: 25px;" :on-remove="handleRemove2" :on-exceed="handleExceed"
					:http-request="myUpload" class="upload-demo" ref="upload2" :data="{value:2}" :limit="1"
					:auto-upload="false">
					<el-button slot="trigger" size="small" type="primary">选取视频</el-button>
					<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload(2)">上传到服务器
					</el-button>
				</el-upload>

				<video controls autoplay muted="muted"
					style="width: 100px;height: 100px;margin-left: 25px;margin-top: 5px;" :src="dialogVedioUrl">
				</video>
				<el-form-item label="课程价格" prop="coursePrice">
					<el-input v-model="ruleForm.coursePrice" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-radio-group v-model="ruleForm.status">
						<el-radio :label="0">未上架</el-radio>
						<el-radio :label="1">上架</el-radio>
						<el-radio :label="2">下架</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible1 = false">取 消</el-button>
				<el-button type="primary" @click="submitForm()">确 定</el-button>
			</div>
		</el-dialog>
		<!--结尾-->

		<!--查看课程-->
		<el-dialog title="查看课程" :visible.sync="dialogFormVisible2">
			<el-form ref="ruleForm" :model="ruleForm" label-width="80px">
				<el-form-item label="课程名称">
					<el-input v-model="ruleForm.courseName" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="课程简介">
					<el-input v-model="ruleForm.descs" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="课程类型">
					<el-select v-model="ruleForm.courseType" placeholder="请选择课程类型">
						<el-option label="JavaEE" :value="1"></el-option>
						<el-option label="数据库" :value="2"></el-option>
						<el-option label="前端技术" :value="3"></el-option>
					</el-select>
				</el-form-item>

				<img :src="dialogImageUrl" style="width: 100px;height: 100px;margin-left: 25px;" alt="" />

				<video controls autoplay muted="muted"
					style="width: 100px;height: 100px;margin-left: 25px;margin-top: 5px;" :src="dialogVedioUrl"></video>
				<el-form-item label="课程价格">
					<el-input v-model="ruleForm.coursePrice" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="ruleForm.status">
						<el-radio :label="0">未上架</el-radio>
						<el-radio :label="1">上架</el-radio>
						<el-radio :label="2">下架</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
		</el-dialog>
		<!--结尾-->
		<!--添加明细-->
		<el-dialog title="课程明细" :visible.sync="dialogFormVisible3">
			<el-form ref="detailInfo" :model="detailInfo" label-width="80px">
				<el-form-item label="课程名称">
					<el-input v-model="detailInfo.name" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="所属课程">
					<el-input v-model="courseName" style="width: 210px;" disabled></el-input>
				</el-form-item>
				<el-form-item label=所属章节>
					<el-select v-model="detailInfo.type" placeholder="请选择所属章节">
						<el-option label="第一章" :value="1"></el-option>
						<el-option label="第二章" :value="2"></el-option>
						<el-option label="第三章" :value="3"></el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="开课时间">
					<el-col :span="7">
						<el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="开课日期"
							v-model="detailInfo.start_data" style="width: 100%;"></el-date-picker>
					</el-col>
				</el-form-item>

				<el-upload style="margin-left: 25px;" :on-remove="handleRemove3" :on-exceed="handleExceed"
					:http-request="myUpload" class="upload-demo" ref="upload3" :data="{value:3}" :limit="1"
					:auto-upload="false">
					<el-button slot="trigger" size="small" type="primary">选取视频</el-button>
					<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload(3)">上传到服务器
					</el-button>
				</el-upload>

				<video controls autoplay muted="muted"
					style="width: 100px;height: 100px;margin-left: 25px;margin-top: 5px;" :src="detailVideoUrl"></video>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible3 = false">取 消</el-button>
				<el-button type="primary" @click="detailForm()">确 定</el-button>
			</div>
		</el-dialog>
		<!--结尾-->
	</div>
</body>
<script>
	axios.defaults.withCredentials = true
	new Vue({
		el: "#app",
		data: {
			uploadImg: '',
			/*表格数据*/
			tableData: [],
			/*条件查询关键字*/
			search: '',
			//批量删除存放选中的复选框
			multipleSelection: [],
			//存放删除的数据
			delarr: [],
			//当前页
			currentPage: 1,
			//每页显示条数
			pageSize: 3,
			//总条数
			totalCount: '',
			//总页数
			totalPage: '',
			//修改的对话框
			dialogFormVisible1: false,
			//详情的对话框
			dialogFormVisible2: false,

			dialogFormVisible3: false,
			/*文件上传*/
			dialogImageUrl: '',
			dialogVedioUrl: '',
			detailVideoUrl: '',
			detailInfo: {
				id: '',
				name: '',
				type: '',
				url: '',
				start_data: '',
				cid: ''
			},
			courseName: '',
			//表单字段
			ruleForm: {
				cid: '',
				courseName: '',
				descs: '',
				courseType: '',
				coursePrice: '',
				courseImage: '',
				courseVideo: '',
				status: '',
				creatTime: ''
			},
			/*校验规则*/
			rules: {
				courseName: [{
					required: true,
					message: '请输入课程名称',
					trigger: 'blur'
				},],
				descs: [{
					required: true,
					message: '请选择课程简介',
					trigger: 'blur'
				}],
				courseType: [{
					required: true,
					message: '请选择课程类型',
					trigger: 'change'
				}],
				coursePrice: [{
					required: true,
					message: '请输入课程价格',
					trigger: 'blur'
				}],
				status: [{
					required: true,
					message: '请至少选择一个状态',
					trigger: 'change'

				}]
			},
			/*选项卡*/
			inputVisible: true,
			disable: false
		},
		methods: {
			uploadSuccess(response, file, fileList) {
			},

			handleClose() {

			},

			handleEdit(index, row) {
				this.dialogFormVisible1 = true;
				this.ruleForm = row;
				this.dialogImageUrl = "http://192.168.194.223:8080/cs_res/course/pic/" + row.courseImage;
				this.dialogVedioUrl = "http://192.168.194.223:8080/cs_res/course/video/" + row.courseVideo;
			},
			resetForm(cid) {
			},

			submitUpload(value) {
				if (value == 1) {
					this.$refs.upload1.submit();
				} else if (value == 2) {
					this.$refs.upload2.submit();
				} else if (value == 3) {
					this.$refs.upload3.submit();
				}
			},

			myUpload(content) {
				var f = new FormData();
				f.append("file", content.file);
				axios({
					url: "http://192.168.194.223:8080/edu/course?flag=upload",
					method: 'post',
					data: f,
					params: {
						value: content.data.value
					}
				}).then(res => {
					if (content.data.value == 1) {
						this.$message.success("上传图片成功！");
						this.dialogImageUrl = "http://192.168.194.223:8080/cs_res/course/pic/" + res.data;
						this.ruleForm.courseImage = res.data;
					} else if (content.data.value == 2) {
						this.$message.success("上传视频成功！");
						this.dialogVedioUrl = "http://192.168.194.223:8080/cs_res/course/video/" + res.data;
						this.ruleForm.courseVideo = res.data;
					} else if (content.data.value == 3) {
						this.$message.success("上传视频成功！");
						this.detailVideoUrl = "http://192.168.194.223:8080/cs_res/course/video/" + res.data;
						this.detailInfo.url = res.data;
					}
				});
			},

			submitForm() {
				this.$refs.ruleForm.validate(res => {
					if (res) {
						axios({
							method: "post",
							url: 'http://192.168.194.223:8080/edu/course',
							data: this.ruleForm,
							params: {
								flag: 'update'
							}
						}).then(res => {
							this.$message.success(res.data);
							this.$refs.upload1.clearFiles();
							this.$refs.upload2.clearFiles();
							this.dialogFormVisible1 = false;
							this.show(this.currentPage, this.pageSize);
						});
					} else {
						this.$message.error("请认真填写表单！");
					}
				});
			},

			handleLook(index, row) {
				this.dialogFormVisible2 = true;
				this.ruleForm = row;
				this.dialogImageUrl = "http://192.168.194.223:8080/cs_res/course/pic/" + row.courseImage;
				this.dialogVedioUrl = "http://192.168.194.223:8080/cs_res/course/video/" + row.courseVideo;
			},

			handleSelectionChange(val) {
				this.delarr = val.map(obj => obj.cid);
			},

			delAll() {
				if (this.delarr.length != 0) {
					axios({
						method: "post",
						url: 'http://192.168.194.223:8080/edu/course',
						data: this.delarr,
						params: {
							flag: 'delCourse'
						}
					}).then(res => {
						this.$message.success(res.data);
						this.currentPage = 1;
						this.show(1, this.pageSize);
					});
				} else {
					this.$message.error("请选择，您要删除的数据！");
				}
			},

			handleSizeChange: function (size) {
				this.pageSize = size;
				this.currentPage = 1;
				this.show(1, size);
			},
			handleCurrentChange: function (currentPage) {
				this.currentPage = currentPage;
				this.show(currentPage, this.pageSize);
			},

			handleRemove1(file, fileList) {
				this.ruleForm.courseImage = '';
				this.dialogImageUrl = '';
				axios.get("http://192.168.194.223:8080/edu/course?flag=remove&value=1&filename=" + this.ruleForm.courseImage).then(res => {
					this.$message.success("取消图片上传！");
				})
			},
			handleRemove2(file, fileList) {
				this.ruleForm.courseVideo = '';
				this.dialogVedioUrl = '';
				axios.get("http://192.168.194.223:8080/edu/course?flag=remove&value=2&filename=" + this.ruleForm.courseVideo).then(res => {
					this.$message.success("取消视频上传！");
				})

			},
			handleRemove3(file, fileList) {
				this.detailInfo.url = '';
				this.detailVideoUrl = '';
				axios.get("http://192.168.194.223:8080/edu/course?flag=remove&value=2&filename=" + this.detailInfo.url).then(res => {
					this.$message.success("取消视频上传！");
				})

			},

			handlePreview(file) {
			},

			handleExceed(files, fileList) {
				this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
			},

			findAll() {
				this.currentPage = 1;
				this.show(1, this.pageSize);
			},

			show(page, size) {
				axios({
					method: 'post',
					url: 'http://192.168.194.223:8080/edu/course',
					params: {
						flag: 'show',
						currentPage: page,
						pageSize: size,
						search: this.search
					}
				}).then(res => {
					this.tableData = res.data.result;
					this.totalCount = res.data.countRows;
				});
			},
			addCourseDetail(index, row) {
				this.dialogFormVisible3 = true;
				this.courseName = row.courseName;
				this.detailInfo.cid = row.cid;
			},
			detailForm() {
				axios({
					method: "post",
					url: 'http://192.168.194.223:8080/edu/courseDetail',
					data: this.detailInfo,
					params: {
						flag: 'add'
					}
				}).then(res => {
					this.$message.success(res.data);
					this.dialogFormVisible3 = false;
					this.detailInfo = {
						id: '',
						name: '',
						type: '',
						url: '',
						start_data: '',
						cid: ''
					}
					this.detailVideoUrl='';
					this.$refs.upload3.clearFiles();
					this.show(this.currentPage, this.pageSize);
				});
			}

		},
		created() {
			this.show(this.currentPage, this.pageSize);
		}
	})
</script>

</html>